<?php
/*
 * @Author: your name
 * @Date: 2020-10-14 16:24:07
 * @LastEditTime: 2021-01-08 15:36:34
 * @LastEditors: Please set LastEditors
 * @Description: 访问日志学习验证器
 * @FilePath: /special/application/adminapiv3/validate/AccessLogLearnValidate.php
 */
namespace app\adminapiv3\validate;

use think\Validate;

class AccessLogLearnValidate extends Validate
{
    protected $rule = [
        //主机配置的数据库id
        'id|学习配置ID' => 'require|number|length:1,10',
        //主机id
        'host_id|主机ID' => 'require|number|length:1,10',
        //学习保持时间 单位(秒)
        'keep_time|学习持续时间' => 'require|number|length:1,10',
        //自学习数据生成规则下发策略 1手动 2自动
        'auto|学习下发策略' => 'require|in:1,2',
        //学习模式 1累计学习 2全新学习
        'model|学习模式' => 'require|in:1,2',
        //生成规则默认处理方式 0拒绝 1允许
        'allow|生成规则默认处理方式' => 'require|in:0,1',
        //是否立即学习 0只生成配置不学习 1生成配置并立即学习
        'now|是否立即学习' => 'require|in:0,1',
        //是否记录访问日志中的 mac地址 0不记录 1记录
        'have_mac_addr|是否记录MAC地址' => 'require|in:0,1',
        //协议 6TCP 17UDP 255全部
        'protocol|网络协议' => 'require|in:6,17,255',
        //协议 6TCP 17UDP 255全部
        'protocols|网络协议' => 'require|in:6,17,255',
        //源ip
        'src_ip|来源地址IP' => 'require|ip',
        //目标ip
        'dest_ip|目标地址IP' => 'require|ip',
        //目标端口
        'dest_port|目标端口' => 'require|between:1,65535',
        //时间
        'ctime|创建时间' => 'require|number',
        //流量方向
        'direction|流量方向' => 'require|in:1,2',
        //学习数据生成规则状态
        'status|学习数据生成规则状态' => 'require|in:0,1',
        //分组值
        'mark|分组值' => 'require|regex:^\w{32}$',
        //学习数据id
        'learn_id|学习数据ID' => 'require|regex:^\d{1,20}(,\d{1,20}){0,30}$',
        //危险端口
        'danger_port|危险端口' => 'regex:([0-9]+[,]?)+',
        //学习结果
        'icon|学习结果' => 'in:1,2,3',
    ];

    //错误信息
    protected $message = [
        'auto.in' => '学习下发策略格式错误',
        'model.in' => '学习模式格式错误',
        'allow.in' => '生成规则默认处理方式格式错误',
        'now.in' => '是否立即学习格式错误',
        'have_mac_addr.in' => '是否记录MAC地址格式错误',
        'protocol.in' => '网络协议格式错误',
        'protocols.in' => '网络协议格式错误',
        'direction.in' => '流量方向格式错误',
        'status.in' => '学习数据生成规则状态格式错误',
        'icon.in' => '学习结果格式错误',
    ];
    
    protected $scene = [
        //修改
        'modify' => ['id','host_id','keep_time','auto','model','allow','now','have_mac_addr','danger_port'],
        //学习开启
        'oneLearnOn' => ['host_id'],
        //学习数据删除
        'handleLearnData' => ['host_id','learn_id'],
        //单个主机id
        'oneHostId' => ['host_id']

    ];

    //数据统计
    public function sceneDataStatisticsFilter ()
    {
        return $this->only(['host_id','protocol','src_ip','dest_ip','dest_port','ctime','direction','host_id'])
        ->remove('src_ip', 'require')
        ->append('src_ip', 'regex:^\d{1,3}(\.\d{1,3}){0,3}$')
        ->remove('dest_ip', 'require')
        ->append('dest_ip', 'regex:^\d{1,3}(\.\d{1,3}){0,3}$')
        ->remove('dest_port', 'require')
        ->remove('ctime', 'require')
        ->remove('direction', 'require')
        ->remove('protocol', 'require');
    }

    /**
     * @name: 批量主机学习
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-11 14:33:58
     */
    public function sceneBulkLearnOn ()
    {
        return $this->only(['host_id','keep_time','auto','model','allow','now','have_mac_addr','danger_port'])
        ->remove('host_id','number|length')
        ->append('host_id', 'regex:^\d+(,\d+){0,30}$');
    }

    //数据列表
    public function sceneDataListFilter ()
    {
       //mark可以不填
       return $this->only(['protocols','src_ip','dest_ip','dest_port','ctime','direction','host_id','status','mark','icon'])
        ->remove('src_ip', 'require')
        ->append('src_ip', 'regex:^\d{1,3}(\.\d{1,3}){0,3}$')
        ->remove('dest_ip', 'require')
        ->append('dest_ip', 'regex:^\d{1,3}(\.\d{1,3}){0,3}$')
        ->remove('dest_port', 'require')
        ->remove('ctime', 'require')
        ->remove('direction', 'require')
        ->remove('protocols', 'require')
        ->remove('status', 'require')
        ->remove('mark','require');
    }

    /**
     * @name: 多个主机id
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-11 14:12:00
     */
    public function sceneBulkHostId()
    {
        return $this->only(['host_id'])->remove('host_id', 'number')->append('host_id','regex:^\d+(,\d+){0,30}$');
    }
}
